package sm.codec.type;

import java.util.ArrayList;
import java.util.List;

import sm.util.Util;

public class Operator implements Cloneable {
	private int operator;

	private List<Object> operands;

	public Operator(int operator, List<Object> operands) {
		this.operator = operator;
		this.operands = operands;
	}

	public int getOperator() {
		return operator;
	}

	public List<Object> getOperands() {
		return operands;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((operands == null) ? 0 : operands.hashCode());
		result = prime * result + operator;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Operator other = (Operator) obj;
		if (operands == null) {
			if (other.operands != null)
				return false;
		} else if (!operands.equals(other.operands))
			return false;
		if (operator != other.operator)
			return false;
		return true;
	}

	@Override
	public Object clone() {
		try {
			Operator v = (Operator) super.clone();
			v.operator = operator;
			if (operands != null) {
				v.operands = new ArrayList<Object>();
				for (Object operand : operands) {
					v.operands.add(Util.cloneData(operand));
				}
			}
			return v;
		} catch (CloneNotSupportedException e) {
			// this shouldn't happen, since we are Cloneable
			throw new InternalError();
		}
	}
}
